/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:hadss_adaptive_layout/hadss_adaptive_layout.dart';

import '../l10n/generated/app_localizations.dart';

class MusicTools {
  List<PopupMenuEntry<String>> buildMenuItemList(
      {required BuildContext context,
      required Map data,
      required Function menuClick}) {
    AppLocalizations il8n = AppLocalizations.of(context)!;
    List<PopupMenuEntry<String>> items = [];
    if (data["select"] == 1) {
      items.add(PopupMenuItem<String>(
        value: 'unCollect',
        child: SizedBox(
          width: 120,
          child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Row(
                  children: [
                    Image.asset(
                      "lib/assets/images/collect_delete.png",
                      width: 20,
                      height: 20,
                    ),
                    const SizedBox(
                      width: 10,
                    ),
                    Text(il8n.cancelCollect)
                  ],
                ),
                const SizedBox(
                  height: 18,
                ),
                Row(
                  children: [
                    const SizedBox(
                      width: 30,
                    ),
                    Expanded(
                      child: Container(
                        color: const Color(0x1a000000),
                        height: 0.5,
                      ),
                    ),
                  ],
                )
              ]),
        ),
        onTap: () {
          menuClick({"type": 'unCollect'});
        },
      ));
    } else {
      items.add(
        PopupMenuItem<String>(
          value: 'collect',
          child: SizedBox(
            width: 120,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Row(
                  children: [
                    Image.asset(
                      "lib/assets/images/heart_unselect.png",
                      width: 20,
                      height: 20,
                    ),
                    const SizedBox(
                      width: 10,
                    ),
                    Text(il8n.collect)
                  ],
                ),
                const SizedBox(
                  height: 18,
                ),
                Row(
                  children: [
                    const SizedBox(
                      width: 30,
                    ),
                    Expanded(
                      child: Container(
                        color: const Color(0x1a000000),
                        height: 0.5,
                      ),
                    ),
                  ],
                )
              ],
            ),
          ),
          onTap: () {
            menuClick({"type": 'collect'});
          },
        ),
      );
    }
    items.add(
      PopupMenuItem<String>(
        value: 'delete',
        child: SizedBox(
          width: 120,
          child: Row(
            children: [
              Image.asset(
                "lib/assets/images/delete.png",
                width: 20,
                height: 20,
              ),
              const SizedBox(
                width: 10,
              ),
              Text(il8n.delete)
            ],
          ),
        ),
        onTap: () {
          menuClick({"type": 'delete'});
        },
      ),
    );
    return items;
  }

  void setPreferredOrientations(
      {required WidthBreakpoint screenType,
      HeightBreakpoint screenHeightType = HeightBreakpoint.sm}) {
    if (screenType == WidthBreakpoint.xs || screenType == WidthBreakpoint.sm) {
      SystemChrome.setPreferredOrientations([
        DeviceOrientation.portraitUp,
      ]);
    } else {
      bool isFold = false;
      if (screenType == WidthBreakpoint.md &&
          screenHeightType == HeightBreakpoint.md) {
        isFold = true;
      }
      //是否是折叠屏
      if (isFold) {
        SystemChrome.setPreferredOrientations([
          DeviceOrientation.portraitUp,
        ]);
      } else {
        SystemChrome.setPreferredOrientations([
          DeviceOrientation.portraitUp,
          DeviceOrientation.portraitDown,
          DeviceOrientation.landscapeLeft,
          DeviceOrientation.landscapeRight,
        ]);
      }
    }
  }
}
